<template>
  <div class="app-container">
    <div
      ref="main"
      class="chart-container"
    />
  </div>
</template>
<script>
import { classesList } from '@/api/classes'
const echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/bar')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: {
        size: 20,
        current: 1,
        total: 1
      },
      legendData: [],
      seriesData: [],
      selected: {}
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.initCharts()
  },
  methods: {
    initCharts() {
      this.chart = echarts.init(this.$refs.main)
      this.genData(12)
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        title: {
          text: '同名数量统计',
          subtext: '纯属虚构',
          x: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          right: 10,
          top: 20,
          bottom: 20,
          data: this.legendData,
          selected: this.selected
        },
        series: [
          {
            name: '姓名',
            type: 'pie',
            radius: '55%',
            center: ['40%', '50%'],
            data: this.seriesData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },
    genData(count) {
      const nameList = [
        '赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章', '云', '苏', '潘', '葛', '奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁', '柳', '酆', '鲍', '史', '唐', '费', '廉', '岑', '薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安', '常', '乐', '于', '时', '傅', '皮', '卞', '齐', '康', '伍', '余', '元', '卜', '顾', '孟', '平', '黄', '和', '穆', '萧', '尹', '姚', '邵', '湛', '汪', '祁', '毛', '禹', '狄', '米', '贝', '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅', '庞', '熊', '纪', '舒', '屈', '项', '祝', '董', '梁', '杜', '阮', '蓝', '闵', '席', '季', '麻', '强', '贾', '路', '娄', '危'
      ]
      for (let i = 0; i < 20; i++) {
        const name = Math.random() > 0.65
          ? this.makeWord(4, 1, nameList) + '·' + this.makeWord(3, 0, nameList)
          : this.makeWord(2, 1, nameList)
        this.legendData.push(name)
        this.seriesData.push({
          name: name,
          value: Math.round(Math.random() * 100000)
        })
        this.selected[name] = i < 8
      }
      console.log(this.legendData)
      console.log(this.seriesData)
      console.log(this.selected)
    },
    makeWord(max, min, nameList) {
      const nameLen = Math.ceil(Math.random() * max + min)
      const name = []
      for (let i = 0; i < nameLen; i++) {
        name.push(nameList[Math.round(Math.random() * nameList.length - 1)])
      }
      return name.join('')
    },
    fetchData() {
      this.listLoading = true
      classesList(this.page).then(response => {
        this.list = response.body
        this.listLoading = false
        this.page.total = response.totalCount
      })
    }
  }
}
</script>
<style scoped>
  .chart-container{
    position: relative;
    width: 100%;
    height: calc(100vh - 84px);
  }
</style>
